home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 16 / CU Amiga Magazine's Super CD-ROM 16 (1997-10-16)(EMAP Images)(GB)[!][issue 1997-11].iso / CUCD / Graphics / Ghostscript / data / type1ops.ps < prev    next >
Text File  |  1997-08-14  |  8KB  |  239 lines

  1. %    Copyright (C) 1992, 1997 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % type1ops.ps
  16. % Define the Type 1 font opcodes for use by Ghostscript utilities.
  17. % This includes both Type 1 and Type 2 CharStrings.
  18.  
  19. % Define the default value of lenIV.
  20.  
  21. /lenIV 4 def
  22.  
  23. % Define the default CharString type.
  24.  
  25. /CharstringType 1 def
  26.  
  27. % ---------------- Encoding ---------------- %
  28.  
  29. /Type1encode 70 dict
  30.  
  31.     % Data types
  32.  
  33. dup /nulltype {
  34.   pop ()
  35. } put
  36. dup /nametype {
  37.   Type1encode exch get
  38. } put
  39. dup /stringtype {
  40. } put
  41. dup /integertype {
  42.   dup dup -107 ge exch 107 le and {
  43.     139 add (x) dup 0 4 -1 roll put
  44.   } {
  45.     dup dup -1131 ge exch 1131 le and {
  46.       dup 0 ge { 16#f694 } { neg 16#fa94 } ifelse add
  47.       (xx) dup dup 0 4 index -8 bitshift put
  48.       1 4 -1 roll 255 and put
  49.     } {
  50.       (\377xxxx) 1 1 4 {
  51.     dup 8 mul 32 sub 3 index exch bitshift 255 and
  52.     2 index 3 1 roll put
  53.       } for exch pop
  54.     } ifelse
  55.   } ifelse
  56. } put
  57.  
  58.     % Operators
  59.  
  60. % Identical or similar in Type 1 and Type 2 CharStrings.
  61. /c_hstem 1 def   dup /hstem <01> put
  62. /c_vstem 3 def   dup /vstem <03> put
  63. /c_vmoveto 4 def   dup /vmoveto <04> put
  64. /c_rlineto 5 def   dup /rlineto <05> put
  65. /c_hlineto 6 def   dup /hlineto <06> put
  66. /c_vlineto 7 def   dup /vlineto <07> put
  67. /c_rrcurveto 8 def   dup /rrcurveto <08> put
  68. /c_callsubr 10 def   /s_callsubr <0a> def   dup /callsubr s_callsubr put
  69. /c_return 11 def   dup /return <0b> put
  70. /c_escape 12 def
  71.   /ce_div 12 def   /s_div <0c0c> def   dup /div s_div put
  72. /c_endchar 14 def   /s_endchar <0e> def   dup /endchar s_endchar put
  73. /c_rmoveto 21 def   dup /rmoveto <15> put
  74. /c_hmoveto 22 def   dup /hmoveto <16> put
  75. /c_vhcurveto 30 def   dup /vhcurveto <1e> put
  76. /c_hvcurveto 31 def   dup /hvcurveto <1f> put
  77. % Only in Type 1 CharStrings.
  78. /c_closepath 9 def   dup /closepath <09> put
  79. /c_hsbw 13 def   /s_hsbw <0d> def   dup /hsbw s_hsbw put
  80.   /ce_dotsection 0 def   /s_dotsection <0c06> def   dup /dotsection s_dotsection put
  81.   /ce_vstem3 1 def   /s_vstem3 <0c01> def   dup /vstem3 s_vstem3 put
  82.   /ce_hstem3 2 def   /s_hstem3 <0c02> def   dup /hstem3 s_hstem3 put
  83.   /ce_seac 6 def   /s_seac <0c06> def    dup /seac s_seac put
  84.   /ce_sbw 7 def   /s_sbw <0c07> def   dup /sbw s_sbw put
  85.   /ce_callothersubr 16 def   /s_callothersubr <0c10> def   dup /callothersubr s_callothersubr put
  86.   /ce_pop 17 def   /s_pop <0c11> def   dup /pop s_pop put
  87.   /ce_setcurrentpoint 33 def   /s_setcurrentpoint <0c21> def   dup /setcurrentpoint s_setcurrentpoint put
  88.   /s_setcurrentpoint_hmoveto s_setcurrentpoint <8b16> concatstrings def
  89. % Only in Type 2 CharStrings.
  90. dup /blend <10> put
  91. dup /hstemhm <12> put
  92. dup /hintmask <13> put
  93. dup /cntrmask <14> put
  94. dup /vstemhm <17> put
  95. dup /rcurveline <18> put
  96. dup /rlinecurve <19> put
  97. dup /vvcurveto <1a> put
  98. dup /hhcurveto <1b> put
  99. dup /callgsubr <1d> put
  100.   dup /and <0c03> put
  101.   dup /or <0c04> put
  102.   dup /not <0c05> put
  103.   dup /store <0c08> put
  104.   dup /abs <0c09> put
  105.   dup /add <0c0a> put
  106.   dup /sub <0c0b> put
  107.   dup /load <0c0d> put
  108.   dup /neg <0c0c> put
  109.   dup /eq <0c0f> put
  110.   dup /drop <0c12> put
  111.   dup /put <0c14> put
  112.   dup /get <0c15> put
  113.   dup /ifelse <0c16> put
  114.   dup /random <0c17> put
  115.   dup /mul <0c18> put
  116.   dup /sqrt <0c1a> put
  117.   dup /dup <0c1b> put
  118.   dup /exch <0c1c> put
  119.   dup /index <0c1d> put
  120.   dup /roll <0c1e> put
  121.   dup /hflex <0c22> put
  122.   dup /flex <0c23> put
  123.   dup /hflex1 <0c24> put
  124.   dup /flex1 <0c25> put
  125.  
  126. readonly def
  127.  
  128. % ---------------- Decoding ---------------- %
  129.  
  130. /Type1decode 512 array
  131.  
  132. Type1encode {
  133.   dup type /stringtype eq {
  134.     dup length 1 eq { 0 get } { 1 get 256 add } ifelse
  135.         % stack: array key code
  136.     exch 2 index 3 1 roll put
  137.   } {
  138.     pop pop
  139.   } ifelse
  140. } forall
  141.  
  142. dup 12 {
  143.   dup read pop dup Type1decode exch 256 add get dup null ne
  144.     { exch pop }
  145.     { pop 2 string dup 0 12 put dup 1 4 -1 roll put }
  146.   ifelse
  147. } put
  148. dup 28 {        % Type 2 CharStrings only
  149.   dup read pop 128 xor 128 sub 8 bitshift
  150.   1 index read pop add
  151. } put
  152. 32 1 246 { 2 copy dup 139 sub put pop } for
  153. dup 247 { dup read pop 108 add } put
  154. dup 248 { dup read pop 364 add } put
  155. dup 249 { dup read pop 620 add } put
  156. dup 250 { dup read pop 876 add } put
  157. dup 251 { dup read pop 108 add neg } put
  158. dup 252 { dup read pop 364 add neg } put
  159. dup 253 { dup read pop 620 add neg } put
  160. dup 254 { dup read pop 876 add neg } put
  161. dup 255 {        % Different for Type 1 and Type 2
  162.   dup read pop 128 xor 128 sub
  163.   3 { 8 bitshift 1 index read pop add } repeat
  164.   CharstringType 2 eq { 65536.0 div } if
  165. } put
  166.  
  167. readonly def
  168.  
  169. % ---------------- Procedures ---------------- %
  170.  
  171. % For these utilities, a CharString is represented by a sequence of
  172. % integers, reals, strings, and names, either in an array or on the stack.
  173. % Integers and reals represent themselves; strings are other data that
  174. % appears in the CharString; names are CharString operator names.
  175. % A CharString in an array is called a "charproc"; a CharString on
  176. % the stack is called a "charstack", and is delimited by a mark.
  177. % Individual elements are called "chartokens".
  178.  
  179. % ------ Encoding ------ %
  180.  
  181. % Get the string for a chartoken.
  182. % Note that this string may be overwritten by the next call.
  183. /chartoken_string {    % <chartoken> chartoken_string <string>
  184.   dup type Type1encode exch get exec
  185. } bind def
  186. % Compute the length of a CharString.
  187. /chartoken_length {    % <chartoken> chartoken_length <length>
  188.   chartoken_string length
  189. } bind def
  190. /charproc_length {    % <charproc> charproc_length <length>
  191.   0 exch { chartoken_length add } forall
  192. } bind def
  193. /charstack_length {    % <charstack> charstack_length <charstack> <length>
  194.   counttomark 0 exch -1 1 { index chartoken_length add } for
  195. } bind def
  196.  
  197. % Write a CharString to a file.  Normally this will be a NullEncode filter
  198. % writing on a string of the correct length.
  199. /chartoken_write {    % <file> <chartoken> chartoken_write -
  200.   chartoken_string writestring
  201. } bind def
  202. /charproc_write {    % <file> <charproc> charproc_write -
  203.   { 1 index exch chartoken_write } forall pop
  204. } bind def
  205. % Note that the arguments of charstack_write are backwards.
  206. /charstack_write {    % <charstack> <file> charstack_write -
  207.   counttomark 1 sub -1 1 { index 1 index exch chartoken_write } for
  208.   cleartomark
  209. } bind def
  210.  
  211. % Convert a charproc or charstack to an *un*encrypted CharString.
  212. /charproc_string {    % <charproc> charproc_string <string>
  213.   mark exch aload pop charstack_string
  214. } bind def
  215. /charstack_string {    % <charstack> charstack_string <string>
  216.   charstack_length lenIV add string
  217.   dup dup length lenIV sub lenIV exch getinterval    % skip lenIV
  218.   /NullEncode filter
  219.   exch 1 index counttomark 1 add 2 roll
  220.   charstack_write closefile
  221. %   4330 exch dup .type1encrypt exch pop readonly
  222. } bind def
  223.  
  224. % ------ Decoding ------ %
  225.  
  226. % Decode a CharString (unencrypted).
  227. /charstack_read {    % <file> charstack_read <no-mark-charstack>
  228.   { dup read not { pop exit } if
  229.     Type1decode exch get dup null eq {
  230.       pop 1 string dup 0 4 -1 roll put
  231.     } {
  232.       exec
  233.     } ifelse exch
  234.   } loop
  235. } bind def
  236.